package jmotioncapture.testing.framework.frameCalculation;

import jmotioncapture.calculation.GeomCalculation;
import jmotioncapture.calculation.FrameCalculation;
import jmotioncapture.camera.Frame;
import jmotioncapture.camera.FrameSet;
import jmotioncapture.geometry2D.Line;
import jmotioncapture.geometry2D.Point;

import java.awt.Graphics;
import java.util.Vector;
import java.awt.geom.Line2D;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class DrawPanel_FrameCalculationTesting extends JPanel {

	final static long serialVersionUID = 1;

	private Frame frame;

	private FrameSet frameSet;

	private boolean withPoint = false;

	private boolean withLabel = false;

	private boolean withLink = false;

	private int[] selectedLines;

	public DrawPanel_FrameCalculationTesting(Frame frame, FrameSet frameSet,
			boolean withPoint, boolean withLabel, boolean withLink,
			int[] selectedLines) {
		this.frame = frame;
		this.withPoint = withPoint;
		this.withLabel = withLabel;
		this.withLink = withLink;
		this.selectedLines = selectedLines;
		this.frameSet = frameSet;
	}

	public void paintComponent(Graphics g) {
		// call paintComponent to ensure the panel displays correctly
		super.paintComponent(g);

		Graphics2D g2 = (Graphics2D) g;

		Vector<Point> points = frame.getPoints();

		// Draw Line
		if (withLink)
			drawLink(points, g2);

		for (int i = 0; i < points.size(); i++) {

			// Get point data
			double p1x = points.elementAt(i).getX();
			double p1y = points.elementAt(i).getY();

			// Draw point
			if (withPoint)
				drawPoint(points.elementAt(i), g2);

			// Draw Label
			if (withLabel)
				g2.drawString("P" + points.elementAt(i).getID()
						+ points.elementAt(i).toString(), ((int) p1x + 10),
						(int) p1y);

		}

	}

	private void drawPoint(Point pt, Graphics2D g2) {
		
		int num = FrameCalculation.NUMBER_OF_FRAMES_DECIDE_STATIC_POINT;
		if(FrameCalculation.isStaticPoint(pt, frameSet.getLastFramesByNumber(num)))
			g2.fillOval((int) pt.getX(), (int) pt.getY(), 10, 10);
		else
			g2.fillOval((int) pt.getX(), (int) pt.getY(), 5, 5);
	}

	private void drawLink(Vector<Point> points, Graphics2D g2) {
		Vector<Line> lines = GeomCalculation.getLines(points);

		if (selectedLines != null && selectedLines.length > 0) {
			for (int i = 0; i < selectedLines.length; i++) {

				g2
						.draw(new Line2D.Double(lines.elementAt(
								selectedLines[i]).getStartPoint().getX(), lines
								.elementAt(selectedLines[i]).getStartPoint()
								.getY(), lines.elementAt(selectedLines[i])
								.getEndPoint().getX(), lines.elementAt(
								selectedLines[i]).getEndPoint().getY()));

				// Draw Label
				if (withLabel)
					g2.drawString("(L" + (selectedLines[i] + 1) + ")",
							((int) lines.elementAt(selectedLines[i])
									.getMidPoint().getX()), (int) lines
									.elementAt(selectedLines[i]).getMidPoint()
									.getY());
			}
		} else {
			for (int i = 0; i < lines.size(); i++) {
				g2.draw(new Line2D.Double(lines.elementAt(i).getStartPoint()
						.getX(), lines.elementAt(i).getStartPoint().getY(),
						lines.elementAt(i).getEndPoint().getX(), lines
								.elementAt(i).getEndPoint().getY()));

				// Draw Label
				if (withLabel)
					g2.drawString("(L" + (i + 1) + ")", ((int) lines.elementAt(
							i).getMidPoint().getX()), (int) lines.elementAt(i)
							.getMidPoint().getY());
			}
		}
	}
}
